//
// Created by Administrator on 2022/1/26.
//

#ifndef CPP_LEARNING_LEETCODEVIEW0207_H
#define CPP_LEARNING_LEETCODEVIEW0207_H
#include <vector>
using namespace std;
#include <math.h>
#include <iostream>

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
 };
class SolutionView0207 {
public:
    int getLen(ListNode *headA){
        int len = 0;
        while (headA){
            headA = headA->next;
            len++;
        }
        return len;
    }
    ListNode * forwordN(ListNode *head, int n){
        while (n--){
            head = head->next;
        }
        return head;
    }

    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        int lenA = getLen(headA);
        int lenB = getLen(headB);

        ListNode *headLong = NULL;
        ListNode *headShort = NULL;
        if(lenA > lenB){
            headLong = forwordN(headA, lenA-lenB);
            headShort = headB;
        }else{
            headLong = forwordN(headB, lenB-lenA);
            headShort = headA;
        }

        while (headLong){
            if(headLong == headShort){
                return headLong;
            }
            headLong = headLong->next;
            headShort = headShort->next;
        }
        return NULL;

    }
};

void testView0207(){
    SolutionView0207 s;

}

#endif //CPP_LEARNING_LEETCODE977_H
